:Init
@setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@pushd %~dp0
@prompt $_
@echo on

if "%~fp1" == "" goto :Usage
if "%~fp2" == "" goto :Usage
goto :CheckPaths

:Usage
@echo Encapsulates the options for copying site files.  
@echo.
@echo COPYSITE source destination
@echo.
@echo   source      Specifies the source directory.
@echo   destination Specifies the target directory.
@echo. 
@echo Locations can be local system paths or UNC shares:
@echo.
@echo   [drive:][path][filename]
@echo   [\\server\share][\path]%
@echo.
@goto End

:CheckPaths
if not exist "%~fp1" (
    @echo Source location "%~fp1" does not exist.
    @goto :End
)

:Base
if exist "%~fp2" (
    if "%~4" == "/onlyOnEmpty" (
        @echo Target exists. Exiting without migrating files.
        @goto :end
    ) 
) else (
    "%WinDir%\System32\cacls.exe" "%~d2" /T /E /G Everyone:F
    @echo Creating directory "%~fp2".
    md "%~fp2"
)

:SetLogFolder
"%WinDir%\System32\cacls.exe" "%TEMP%" /T /E /G Everyone:F
set _logFile=%TEMP%\accel.copySites.log
goto :SyncFiles

@echo Starting file migration using the following configuration:
@echo.
@echo   "%~fp1"
@echo               Source directory.
@echo   "%~fp2"
@echo               Target directory.
@echo   /LOG:+"%_logFile%"
@echo               Log file (append).
@echo   /e          Copy subdirs (including empty)
@echo   /a-:R       Remove the read-only attribute.
@echo   /ZB         Copy using restartable mode; if access denied use backup mode.
@echo   /COPY:DATS  Copy data, attributes, timestamps, NTFS ACLSs
@echo   /SECFIX     Fix file security on all files, even skipped files.
@echo   /XO         Exclude older files.

:SyncFiles
"%WinDir%\System32\robocopy.exe" "%~fp1" "%~fp2" /e /a-:R /ZB /COPY:DATS /SECFIX /XO /LOG:+"accel.copySite.log" /TEE

:Acls
"%WinDir%\System32\icacls.exe" "%~fp2" /grant "NETWORK SERVICE":(OI)(CI)M
"%WinDir%\System32\icacls.exe" "%~fp2" /grant "NETWORK SERVICE":(OI)(CI)M
"%WinDir%\System32\icacls.exe" "%~fp2" /grant "IIS_IUSRS":(OI)(CI)R

:Umbraco
"%WinDir%\System32\icacls.exe" "%~fp2\~" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\app_code" /grant "IIS APPPOOL\%~3":(OI)(CI)RX 
"%WinDir%\System32\icacls.exe" "%~fp2\~\app_browsers" /grant "IIS APPPOOL\%~3":(OI)(CI)RX 
"%WinDir%\System32\icacls.exe" "%~fp2\~\app_data" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\bin" /grant "IIS APPPOOL\%~3":(OI)(CI)R 
"%WinDir%\System32\icacls.exe" "%~fp2\~\config" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\css" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\data" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\masterpages" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\media" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\python" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\scripts" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\umbraco" /grant "IIS APPPOOL\%~3":(OI)(CI)R 
"%WinDir%\System32\icacls.exe" "%~fp2\~\usercontrols" /grant "IIS APPPOOL\%~3":(OI)(CI)R 
"%WinDir%\System32\icacls.exe" "%~fp2\~\xslt" /grant "IIS APPPOOL\%~3":(OI)(CI)M 
"%WinDir%\System32\icacls.exe" "%~fp2\~\web.config" /grant "IIS APPPOOL\%~3":(OI)(CI)M 

@REM ~~( END )~~
:End
@echo Completed.